package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"shang/conmon"
	"shang/models"
	"strconv"
	"time"
)

type LoginController struct {
	beego.Controller
}

func (l *LoginController) Login() {
	username := l.GetString("username")
	password := l.GetString("password")
	user, _ := models.GetUserInfo(models.UserTwoWeek{
		Username: username,
	})
	if user.ID == 0 {
		l.Data["json"] = map[string]interface{}{
			"code": 400,
			"msg":  "账号不存在",
		}
		l.ServeJSON()
		return
	}
	if user.Password != password {
		l.Data["json"] = map[string]interface{}{
			"code": 400,
			"msg":  "密码错误",
		}
		l.ServeJSON()
		return
	}
	token, _ := conmon.GetJwtToken("shangxinping", time.Now().Unix(), 3600, strconv.Itoa(int(user.ID)))
	if token == "" {
		l.Data["json"] = map[string]interface{}{
			"code": 400,
			"msg":  "token不能为空",
		}
		l.ServeJSON()
		return
	}
	l.Data["json"] = map[string]interface{}{
		"code":  200,
		"msg":   "登录成功",
		"token": token,
	}
	l.ServeJSON()
}
